﻿#include "ordersetterdlg.h"
#include "ui_ordersetterdlg.h"

OrderSetterDlg::OrderSetterDlg(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::OrderSetterDlg)
{

    ui->setupUi(this);

    m_ChangeOrderDlg = new ChangeOrderDlg();
    m_FastOrderDlg   = new FastOrderDlg();
    m_OrderCntDlg    = new OrderCntDlg();
    m_OrderTimesDlg  = new OrderTimesDlg();

    ui->stackedWidget->insertWidget(0,m_ChangeOrderDlg);
    ui->stackedWidget->insertWidget(1,m_FastOrderDlg);
    ui->stackedWidget->insertWidget(2,m_OrderTimesDlg);
    ui->stackedWidget->insertWidget(3,m_OrderCntDlg);
    ui->stackedWidget->setCurrentWidget(m_ChangeOrderDlg);

    setWindowTitle(QStringLiteral("报单设置"));
    ui->listWidget->insertItem(0,QStringLiteral("键盘撤单改单"));
    ui->listWidget->insertItem(1,QStringLiteral("键盘快速下单"));
    ui->listWidget->insertItem(2,QStringLiteral("下单数量倍率"));
    ui->listWidget->insertItem(3,QStringLiteral("报单相关设置"));

    ui->listWidget->setAlternatingRowColors(true);
    connect(ui->listWidget,SIGNAL(currentRowChanged(int)),this,SLOT(SelectDlg(int)));
    connect(ui->m_OKButton,SIGNAL(clicked()),this,SLOT(OKButtomClicked()));

    connect(this,SIGNAL(FastOrderOK()),m_FastOrderDlg,SLOT(OKButtonClicked()));
    connect(this,SIGNAL(ChangeOrderOK()),m_ChangeOrderDlg,SLOT(OKButtonClicked()));
    connect(this,SIGNAL(OrderTimesOK()),m_OrderTimesDlg,SLOT(OKButtonClicked()));
    connect(this,SIGNAL(OrderSettingsOK()),m_OrderCntDlg,SLOT(OKButtonClicked()));

    connect(m_FastOrderDlg,SIGNAL(SigJudgeFastKeyString(QString)),this,SLOT(SlotJudgeFastKeyString(QString)));
    connect(m_ChangeOrderDlg,SIGNAL(SigJudgeChangeKeyString(QString)),this,SLOT(SlotJudgeChangeKeyString(QString)));
    connect(m_OrderTimesDlg,SIGNAL(SigJudgeTimesKeyString(QString)),this,SLOT(SlotJudgeTimesKeyString(QString)));

    connect(this,SIGNAL(SigJudgeFastKeyString(QString)),m_FastOrderDlg,SLOT(SlotJudgeFastKeyString(QString)));
    connect(this,SIGNAL(SigJudgeChangeKeyString(QString)),m_ChangeOrderDlg,SLOT(SlotJudgeChangeKeyString(QString)));
    connect(this,SIGNAL(SigJudgeTimesKeyString(QString)),m_OrderTimesDlg,SLOT(SlotJudgeTimesKeyString(QString)));

    connect(m_ChangeOrderDlg,SIGNAL(SigHideCancelOrderButtonToggled(bool)),this,SLOT(SlotHideCancelOrderButtonToggled(bool)));
}

OrderSetterDlg::~OrderSetterDlg()
{
    delete ui;
}
void OrderSetterDlg::SlotHideCancelOrderButtonToggled(bool b){
    emit SigHideCancelOrderButtonToggled(b);
}
void OrderSetterDlg::SlotJudgeChangeKeyString(QString key){
    QString msg = "";
    if(m_FastOrderDlg->JudgeKeyStringFlg(key)){
        msg = QStringLiteral("快捷键 ") + key + QStringLiteral(" 已在 键盘快速下单 中使用! ");
        emit SigJudgeChangeKeyString(msg);
        return;
    }
    if(m_OrderTimesDlg->JudgeKeyStringFlg(key)){
        msg = QStringLiteral("快捷键 ") + key + QStringLiteral(" 已在 下单数量倍率 中使用! ");
        emit SigJudgeChangeKeyString(msg);
        return;
    }
}
void OrderSetterDlg::SlotJudgeFastKeyString(QString key){
    QString msg = "";
    if(m_ChangeOrderDlg->JudgeKeyStringFlg(key)){
        msg = QStringLiteral("快捷键 ") + key + QStringLiteral(" 已在 键盘撤单改单 中使用! ");
        emit SigJudgeFastKeyString(msg);
        return;
    }
    if(m_OrderTimesDlg->JudgeKeyStringFlg(key)){
        msg = QStringLiteral("快捷键 ") + key + QStringLiteral(" 已在 下单数量倍率 中使用! ");
        emit SigJudgeFastKeyString(msg);
        return;
    }
}
void OrderSetterDlg::SlotJudgeTimesKeyString(QString key){
    QString msg = "";
    if(m_FastOrderDlg->JudgeKeyStringFlg(key)){
        msg = QStringLiteral("快捷键 ") + key + QStringLiteral(" 已在 键盘快速下单 中使用! ");
        emit SigJudgeTimesKeyString(msg);
        return;
    }
    if(m_ChangeOrderDlg->JudgeKeyStringFlg(key)){
        msg = QStringLiteral("快捷键 ") + key + QStringLiteral(" 已在 键盘撤单改单 中使用! ");
        emit SigJudgeTimesKeyString(msg);
        return;
    }
}
void OrderSetterDlg::SelectDlg(int index){
    ui->stackedWidget->setCurrentIndex(index);
}
void OrderSetterDlg::OKButtomClicked(){
    /*switch(ui->stackedWidget->currentIndex()){
    case 0:
        emit ChangeOrderOK();
        break;
    case 1:
        emit FastOrderOK();
        break;
    case 2:
        emit OrderTimesOK();
        break;
    case 3:
        emit OrderSettingsOK();
        break;
    default:
        break;
    }*/
    emit ChangeOrderOK();
    emit FastOrderOK();
    emit OrderTimesOK();
    emit OrderSettingsOK();
    this->accept();
}

bool OrderSetterDlg::GetChangeOrderFlg(){
    return m_ChangeOrderDlg->GetChangeOrderFlg();
}
bool OrderSetterDlg::GetFastOrderFlg(){
    return m_FastOrderDlg->GetFastOrderFlg();
}
bool OrderSetterDlg::GetPingJinCangFlg(){
    return m_OrderCntDlg->GetPingJinCangFlg();
}
bool OrderSetterDlg::GetCancelOrderConfirmFlg(){
    return m_ChangeOrderDlg->GetCancelOrderConfirmFlg();
}
bool OrderSetterDlg::GetCancelOrderDoubleClickedFlg(){
    return m_ChangeOrderDlg->GetCancelOrderDoubleClickedFlg();
}
bool OrderSetterDlg::GetClosePositionFlg(){
    return m_OrderCntDlg->GetClosePositionFlg();
}
QString  OrderSetterDlg::GetClosePositionPriceType(){
    return m_OrderCntDlg->GetClosePositionPriceType();
}
QString  OrderSetterDlg::GetClosePositionCntType(){
    return m_OrderCntDlg->GetClosePositionCntType();
}

int OrderSetterDlg::GetDefaultOrderCnt(){
    return m_OrderCntDlg->GetDefaultOrderCnt();
}

void OrderSetterDlg::OrderDefaultAction(QKeyEvent *k){
    QString keyString = GetKeyString(k);
    FastInfo* pFastInfo = m_FastOrderDlg->GetFastInfo(keyString);
    if(pFastInfo != NULL && GetFastOrderFlg()){//快捷下单
        emit SigFastOrder(pFastInfo);
        return;
    }
    int iChangeType = m_ChangeOrderDlg->GetChangeType(keyString);
    if(iChangeType != -1 && GetChangeOrderFlg()){//改撤单
        emit SigChangeOrder(iChangeType);
        return;
    }
    int iOrderTimes = m_OrderTimesDlg->GetOrderTimes(keyString);
    if(iOrderTimes != -1){//更改下单倍数
        emit SigSetHandsTimes(iOrderTimes);
        return;
    }
}

QString OrderSetterDlg::GetKeyString(QKeyEvent *k){
    switch(k->key()){
    case Qt::Key_1:
        if (k->nativeVirtualKey() == 97){
            return QStringLiteral("小键盘 1");
        }else if(k->nativeVirtualKey() == 49){
            return QStringLiteral("1");
        }
        break;
    case Qt::Key_2:
        if (k->nativeVirtualKey() == 98){
            return QStringLiteral("小键盘 2");
        }else if(k->nativeVirtualKey() == 50){
            return QStringLiteral("2");
        }
        break;
    case Qt::Key_3:
        if(k->nativeVirtualKey() == 99){
            return QStringLiteral("小键盘 3");
        }else if(k->nativeVirtualKey() == 51 ){
            return QStringLiteral("3");
        }
        break;
    case Qt::Key_4:
        if(k->nativeVirtualKey() == 100){
            return QStringLiteral("小键盘 4");
        }else if(k->nativeVirtualKey() == 52 ){
            return QStringLiteral("4");
        }
        break;
    case Qt::Key_5:
        if(k->nativeVirtualKey() == 101){
            return QStringLiteral("小键盘 5");
        }else if(k->nativeVirtualKey() == 53 ){
            return QStringLiteral("5");
        }
        break;
    case Qt::Key_6:
        if(k->nativeVirtualKey() == 102 ){
            return QStringLiteral("小键盘 6");
        }else if(k->nativeVirtualKey() == 54 ){
            return QStringLiteral("6");
        }
        break;
    case Qt::Key_7:
        if(k->nativeVirtualKey() == 103 ){
            return QStringLiteral("小键盘 7");
        }else if(k->nativeVirtualKey() == 55 ){
            return QStringLiteral("7");
        }
        break;
    case Qt::Key_8:
        if(k->nativeVirtualKey() == 104 ){
            return QStringLiteral("小键盘 8");
        }else if(k->nativeVirtualKey() == 56 ){
            return QStringLiteral("8");
        }
        break;
    case Qt::Key_9:
        if(k->nativeVirtualKey() == 105 ){
            return QStringLiteral("小键盘 9");
        }else if(k->nativeVirtualKey() == 57 ){
            return QStringLiteral("9");
        }
        break;
    case Qt::Key_A:
        return QStringLiteral("A");
    case Qt::Key_B:
        return QStringLiteral("B");
    case Qt::Key_C:
        return QStringLiteral("C");
    case Qt::Key_D:
        return QStringLiteral("D");
    case Qt::Key_E:
        return QStringLiteral("E");
    case Qt::Key_F:
        return QStringLiteral("F");
    case Qt::Key_G:
        return QStringLiteral("G");
    case Qt::Key_H:
        return QStringLiteral("H");
    case Qt::Key_I:
        return QStringLiteral("I");
    case Qt::Key_J:
        return QStringLiteral("J");
    case Qt::Key_K:
        return QStringLiteral("K");
    case Qt::Key_L:
        return QStringLiteral("L");
    case Qt::Key_M:
        return QStringLiteral("M");
    case Qt::Key_N:
        return QStringLiteral("N");
    case Qt::Key_O:
        return QStringLiteral("O");
    case Qt::Key_P:
        return QStringLiteral("P");
    case Qt::Key_Q:
        return QStringLiteral("Q");
    case Qt::Key_R:
        return QStringLiteral("R");
    case Qt::Key_S:
        return QStringLiteral("S");
    case Qt::Key_T:
        return QStringLiteral("T");
    case Qt::Key_U:
        return QStringLiteral("U");
    case Qt::Key_V:
        return QStringLiteral("V");
    case Qt::Key_W:
        return QStringLiteral("W");
    case Qt::Key_X:
        return QStringLiteral("X");
    case Qt::Key_Y:
        return QStringLiteral("Y");
    case Qt::Key_Z:
        return QStringLiteral("Z");
    case Qt::Key_F1:
        return QStringLiteral("F1");
    case Qt::Key_F2:
        return QStringLiteral("F2");
    case Qt::Key_F3:
        return QStringLiteral("F3");
    case Qt::Key_F4:
        return QStringLiteral("F4");
    case Qt::Key_F5:
        return QStringLiteral("F5");
    case Qt::Key_F6:
        return QStringLiteral("F6");
    case Qt::Key_F7:
        return QStringLiteral("F7");
    case Qt::Key_F8:
        return QStringLiteral("F8");
    case Qt::Key_F9:
        return QStringLiteral("F9");
    case Qt::Key_F10:
        return QStringLiteral("F10");
    case Qt::Key_F11:
        return QStringLiteral("F11");
    case Qt::Key_F12:
        return QStringLiteral("F12");
    case Qt::Key_Space:
        return QStringLiteral("空格键");
    case Qt::Key_Return:
    case Qt::Key_Enter:
        return QStringLiteral("回车键");
    case Qt::Key_Alt:
        return QStringLiteral("Alt键");
    case Qt::Key_Control:
        return QStringLiteral("Ctrl键");
    case Qt::Key_Up:
        return QStringLiteral("↑");
    case Qt::Key_Down:
        return QStringLiteral("↓");
    case Qt::Key_Left:
        return QStringLiteral("←");
    case Qt::Key_Right:
        return QStringLiteral("→");
    case Qt::Key_Minus:
        return QStringLiteral("减号");
    case Qt::Key_Plus:
        return QStringLiteral("加号");
    case Qt::Key_Equal:
        return QStringLiteral("等号");
        //m_KeyCombox->addItem(QStringLiteral("乘号"));
        //m_KeyCombox->addItem(QStringLiteral("除号"));
    case Qt::Key_Delete:
        return QStringLiteral("删除键");
    case Qt::Key_Insert:
        return QStringLiteral("插入键");
    default:
        return QStringLiteral("");
    }
}
